﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

using SUMO.MediaBrowser.Foundation;

namespace SUMO.MediaBrowser
{
    public partial class UmbracoImageEditorControl : ChildWindow, IUmbracoImageEditor
    {

        public event ImageEditedEventHandler ImageEdited;
       

        protected virtual void OnImageEdited(ImageEditedEventArgs e)
        {
            if (ImageEdited != null)
            {
                // Invokes the delegates. 
                ImageEdited(this, e);
            }
        }

        private bool _modified = false;

        public string EditorName
        {
            get { return "Base Editor Control"; }
        }

        public UmbracoMediaItem MediaItem
        {
            get;
            set;
        }

        public bool Modified
        {
            get { return _modified; }
        }

        public UmbracoImageEditorControl(UmbracoMediaItem m)
        {
            InitializeComponent();
            this.MediaItem = m;
            this.DataContext = this.MediaItem;
            this.Title = this.EditorName;

        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            /* WriteableBitmap b = new WriteableBitmap((int) EditorImage.ActualWidth, (int)EditorImage.ActualHeight);
            b.Render(EditorImage, new TranslateTransform());
            
            WriteableBitmap b = SetupRenderedTextBitmap("Umbraco", 40);
            */

            // EditorImage.Source = b;

            ImageEditedEventArgs ev = new ImageEditedEventArgs(this.MediaItem);
            ev.EditedImage = this.EditorImage;


            OnImageEdited(ev);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private WriteableBitmap SetupRenderedTextBitmap(string text, double fontSize)
        {
            // setup the textblock we will render to a bitmap
            TextBlock txt1 = new TextBlock();

            txt1.Text = text;

            txt1.FontSize = fontSize; // set the font size before using the Actual Width / Height



            // create our first bitmap we will render to

            WriteableBitmap bitmap = new WriteableBitmap((int)txt1.ActualWidth,
               (int)txt1.ActualHeight);



            // put a black textblock under the white one to create a simple dropshadow

            txt1.Foreground = new SolidColorBrush(Colors.Black);

            bitmap.Render(txt1, new TranslateTransform() { X = -2, Y = -2 });



            txt1.Foreground = new SolidColorBrush(Colors.White);

            bitmap.Render(txt1, new TranslateTransform());



            // invalidate the bitmap so that it is rendered

            bitmap.Invalidate();


            return bitmap;
        }

        private void DoButton_Click(object sender, RoutedEventArgs e)
        {
            // WriteableBitmap b = SetupRenderedTextBitmap("Umbraco", 40);
            
            WriteableBitmap b = new WriteableBitmap((int)EditorImage.ActualWidth, (int)EditorImage.ActualHeight);
            b.Render(EditorImage, new TranslateTransform());

            TextBlock txt1 = new TextBlock();
            txt1.Text = "Umbraco";
            txt1.FontSize = 40;
            txt1.Foreground = new SolidColorBrush(Colors.Black);
            txt1.Margin = new Thickness(10);
            
            b.Render(txt1, new TranslateTransform());
            b.Invalidate();

            EditorImage.Source = b;
        }
    }

    
}

